﻿using System;
using Avalonia.Controls;
using iOSClub.App.Share;

namespace iOSClub.App.Desktop.ProjectDialogs;

public partial class AddOrChangeTask : UserControl
{
    public AddOrChangeTask(TaskModel? model = null)
    {
        InitializeComponent();
        if (model == null)
        {
            StartPicker.SelectedDate = EndPicker.SelectedDate = DateTime.Today;            
        }
        else
        {
            NameBox.Text = model.Description;
            StartPicker.SelectedDate = DateTime.Parse(model.StartTime);
            EndPicker.SelectedDate = DateTime.Parse(model.EndTime);
        }

    }

    public TaskModel Done()
    {
        if (string.IsNullOrEmpty(NameBox.Text) || StartPicker.SelectedDate == null || EndPicker.SelectedDate == null)
            return new TaskModel();
        return new TaskModel()
        {
            Description = NameBox.Text, StartTime = StartPicker.SelectedDate.Value.ToString("yyyy-MM-dd"),
            EndTime = EndPicker.SelectedDate.Value.ToString("yyyy-MM-dd")
        };
    }
}